//package com.ruoqing.gateway.filter;
//
//import com.ruoqing.gateway.utils.JsonUtils;
//import com.ruoqing.gateway.vo.AdminResponseVo;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.cloud.gateway.route.Route;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import java.net.URI;
//import java.nio.charset.StandardCharsets;
//import java.util.Collections;
//import java.util.Objects;
//import java.util.Set;
//
//import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.*;
//
///**
//	* @author YaoXian
//	* @title: AdminTokenFilter
//	* @projectName ruoxi-blog
//	* @description: 若兮微服务 - 网关全局过滤器 - 对后台管理中心访问稍作限制
//	* @date 2020-12-14 12:16
//	*/
//@Slf4j
//@Component
//public class AdminTokenFilter implements GlobalFilter {
//		@Override
//		public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//				Set<URI> uris = exchange.getAttributeOrDefault(GATEWAY_ORIGINAL_REQUEST_URL_ATTR, Collections.emptySet());
//				String originalUri = (uris.isEmpty()) ? "Unknown" : uris.iterator().next().toString();
//				Route route = exchange.getAttribute(GATEWAY_ROUTE_ATTR);
//				URI routeUri = exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR);
//				log.info("Incoming request " + originalUri + " is routed to id: " + route.getId()
//								+ ", uri:" + routeUri);
//				if (originalUri.contains("admin") || routeUri.toString().contains("9000")){
//						if (exchange.getRequest().getQueryParams().getFirst("admin_token") == null){
//								ServerHttpResponse response = exchange.getResponse();
//								AdminResponseVo adminResponseVo = new AdminResponseVo();
//								adminResponseVo.setCode(401);
//								adminResponseVo.setMsg("您没有当前资源的访问权限!");
//								byte[] data = Objects.requireNonNull(JsonUtils.objectToJson(adminResponseVo)).getBytes(StandardCharsets.UTF_8);
//								DataBuffer buffer = response.bufferFactory().wrap(data);
//								response.setStatusCode(HttpStatus.UNAUTHORIZED);
//								response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
//								return response.writeWith(Mono.just(buffer));
//						}
//				}
//				return chain.filter(exchange);
//		}
//}
